//
//  AY_BookPChasePage.m
//  AiYue
//
//  Created by boWen on 2023/11/2.
//

#import "AY_BookPChasePage1.h"
#import "AY_BookChaseCell.h"
@interface AY_BookPChasePage1 ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView * baseTableView;

@property (nonatomic ,assign) NSInteger currentIndex;
@property (nonatomic ,strong) UIButton *sureBtn;
@property (nonatomic ,strong) UIViewController *vc;
@property (nonatomic ,strong) NSArray *dataArr;
@property (nonatomic ,copy) NSString *totalBalance;
@property (nonatomic ,copy) NSString *pType;
@property (nonatomic ,strong) VpProductModel *proModel;
@end

@implementation AY_BookPChasePage1

- (instancetype)initTotalPy:(NSString *)totalBalance vc:(UIViewController *)vc dataSource:(NSArray *)dataSource andPtype:(NSString *)pType andInfo:(VpProductModel *)model {
    if (self = [super init]) {
        self.vc = vc;
        self.totalBalance = totalBalance;
        self.dataArr = dataSource;
        self.pType = pType;
        self.proModel = model;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gk_navigationBar.hidden = YES;
    self.currentIndex = [self.pType isEqual:@"ZFB"] ? 0 : 1;
    [self initPop];
    [self setUpUI];
}



- (void)initPop {
    self.view.backgroundColor = [UIColor whiteColor];
    CGFloat height = YNUM(154);
    height += self.dataArr.count * YNUM(62);
    self.contentSizeInPopup = CGSizeMake(self.view.frame.size.width, height);
    self.popupController.navigationBarHidden = YES;
    self.popupController.containerView.layer.masksToBounds = YES;
    self.popupController.containerView.layer.cornerRadius = YNUM(12);
    [self.popupController.backgroundView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTap)]];
}

- (void)setUpUI {
    [self.view addSubview:self.baseTableView];
    [self creatTopView];
}

-(void)closeBlockView {
    [self backgroundTap];
}

- (void)backgroundTap  {
    [self.popupController dismiss];
}


- (UITableView *)baseTableView {
    if (!_baseTableView) {
        UITableView * myTableView = [AYTool creatMyTableView:self.view.frame];
        myTableView.layer.masksToBounds = YES;
        myTableView.layer.cornerRadius = YNUM(8);
        myTableView.delegate = self;
        myTableView.dataSource = self;
        myTableView.scrollEnabled = NO;
        myTableView.backgroundColor = [UIColor clearColor];
        [myTableView registerClass:[AY_BookChaseCell class] forCellReuseIdentifier:@"AY_BookChaseCell"];
        _baseTableView = myTableView;
    }
    return _baseTableView;
}




#pragma mark - UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AY_BookChaseCell * cell = [tableView dequeueReusableCellWithIdentifier:@"AY_BookChaseCell"];
    [self configCell:cell data:[self.dataArr objectAtIndex:indexPath.row] indexPath:indexPath];
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return YNUM(62);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.currentIndex = indexPath.row;
    [self.baseTableView reloadData];
    if (self.pyType) {
        self.pyType([[self.dataArr objectAtIndex:self.currentIndex] objectForKey:@"type"],self.totalBalance);
        [self backgroundTap];
    }
}


- (void)configCell:(AY_BookChaseCell *)cell data:(NSDictionary *)data indexPath:(NSIndexPath *)indexPath{
    [cell.iconImgView sd_setImageWithURL:[NSURL URLWithString:kStringUtils(data[@"typeCoverUrl"])] placeholderImage:YImage(@"placeholderImage")];
    NSString *str = kStringUtils(data[@"typeName"]);
    cell.titleLabel.text = str;
    
    if ([self.proModel.tipStr isEqual:@"0"] || [self.proModel.tipStr isEqual:@"10"]) {
        cell.infoLabel.text = @"   极速到账   ";
    }else {
        cell.infoLabel.text = [NSString stringWithFormat:@"   限时%@折   ",self.proModel.tipStr];
    }
    
    if ([data[@"type"] isEqual:@"ZFB"]) {
        cell.infoLabel.hidden = NO;
    }else {
        cell.infoLabel.hidden = YES;
    }
    
    if (self.currentIndex == indexPath.row) {
        cell.stateView.image =  YImage(@"py_selected");
    }else{
        cell.stateView.image =  YImage(@"");
    }
}

- (void)creatTopView {
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, YNUM(64))];
    topView.layer.masksToBounds = YES;
    topView.layer.cornerRadius = YNUM(8);
    UILabel * titleLabel = [AYTool createLabelWithFrame:CGRectZero andText:[NSString stringWithFormat:@"选择%@方式",[AYMMKV getStringForKey:ZF]] andTextColor:COLOR(@"#010101") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(16)] andLines:1 andRadius:0 andAddSubView:topView];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(topView);
        make.top.equalTo(topView).offset(YNUM(16));
        make.height.mas_offset(YNUM(16));
    }];
    
    UIButton *xButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [xButton setImage:YImage(@"AY_close_icon") forState:UIControlStateNormal];
    [topView addSubview:xButton];
    [xButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topView).offset(YNUM(16));
        make.right.equalTo(topView).offset(YNUM(-12));
        make.height.width.mas_offset(YNUM(24));
    }];
    [xButton addTarget:self action:@selector(backgroundTap) forControlEvents:UIControlEventTouchUpInside];
    self.baseTableView.tableHeaderView = topView;
}



@end
